
package com.hulk.dryad.admin.controller;

import com.hulk.dryad.admin.api.vo.SysNoticeVO;
import com.hulk.dryad.admin.mapstruct.SysNoticeStruct;

import com.hulk.dryad.admin.controller.basic.IController;

import com.hulk.dryad.persistence.entity.SysNotice;
import com.hulk.dryad.manage.provider.service.SysNoticeService;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hulk.dryad.common.component.R;
import com.hulk.dryad.common.annotation.PrintlnLog;
import com.hulk.dryad.common.annotation.OperLog;
import com.hulk.dryad.common.constant.enums.ActionType;
import com.hulk.dryad.common.annotation.Idempotent;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

/**
 * 通知公告
 *
 * @author hulk
 * @date 2021-04-01 14:07:32
 */
@Api(value = "admin_sysnotice", tags = "通知公告管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/sysnotice" )
public class SysNoticeController implements IController{

    private final  SysNoticeService sysNoticeService;

    /**
     * 分页查询通知公告
     * @param page 分页对象
     * @param vo 通知公告
     * @return
     */
    @ApiOperation(value = "分页查询通知公告", notes = "分页查询通知公告")
    @PrintlnLog
    @GetMapping("/page" )
    public R<IPage> getSysNoticePage(Page page, SysNoticeVO vo) {
        SysNotice model = SysNoticeStruct.INSTANCE.to(vo);
        Page<SysNotice> rpm =  sysNoticeService.page(page, Wrappers.query(model));
        Page<SysNoticeVO>  rpv = SysNoticeStruct.INSTANCE.from(rpm);
        return R.ok(rpv);
    }


    /**
     * 通过id查询通知公告
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询通知公告", notes = "通过id查询通知公告")
    @PrintlnLog
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Long id) {
        SysNotice model = sysNoticeService.getById(id);
        SysNoticeVO vo = SysNoticeStruct.INSTANCE.from(model);
        return R.ok(vo);
    }

    /**
     * 新增通知公告
     * @param vo 通知公告
     * @return R
     */
    @ApiOperation(value = "新增通知公告", notes = "新增通知公告")
    @OperLog(value = "新增通知公告", action = ActionType.ADD)
    @Idempotent
    @PostMapping
    @PreAuthorize("@pms.hasPermission('admin_sysnotice_add')" )
    public R save(@Valid @RequestBody SysNoticeVO vo) {
        SysNotice model = SysNoticeStruct.INSTANCE.to(vo);
        return R.ok(sysNoticeService.save(model));
    }

    /**
     * 通过id修改通知公告
     * @param vo 通知公告
     * @return R
     */
    @ApiOperation(value = "通过id修改通知公告", notes = "通过id修改通知公告")
    @OperLog(value ="通过id修改通知公告", action = ActionType.UPDATE)
    @Idempotent
    @PutMapping
    @PreAuthorize("@pms.hasPermission('admin_sysnotice_edit')" )
    public R updateById(@Valid @RequestBody SysNoticeVO vo) {
        SysNotice model = SysNoticeStruct.INSTANCE.to(vo);
        return R.ok(sysNoticeService.updateById(model));
    }

    /**
     * 通过id删除通知公告
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除通知公告", notes = "通过id删除通知公告")
    @OperLog(value = "通过id删除通知公告", action = ActionType.DEL)
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('admin_sysnotice_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(sysNoticeService.removeById(id));
    }

}
